package drds.binlog.parse.mysql.table_meta_data;

import com.google.common.collect.Maps;
import drds.binlog.common.Authors;
import drds.common.Author;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentMap;


@Slf4j
@Author(name = Authors.LI_YANG)
public class TableMetaDataGetterBuilder
{

    private static ConcurrentMap<String, TableMetaDataGetter> destinationToTableMetaDataGetterMap = Maps.newConcurrentMap();

    /**
     * 代理一下tableMetaTSDB的获取,使用隔离的spring定义
     */
    public static TableMetaDataGetter build(String destination)
    {
        TableMetaDataGetter tableMetaDataGetter = destinationToTableMetaDataGetterMap.get(destination);
        if (tableMetaDataGetter == null)
        {
            synchronized (destinationToTableMetaDataGetterMap)
            {
                if (tableMetaDataGetter == null)
                {
                    tableMetaDataGetter = new BaseOnDatabaseTableMetaDataGetter();
                    destinationToTableMetaDataGetterMap.put(destination, tableMetaDataGetter);
                }
            }
        }

        tableMetaDataGetter.init(destination);

        return tableMetaDataGetter;
    }

    public static void destory(String destination)
    {
        TableMetaDataGetter tableMetaDataGetter = destinationToTableMetaDataGetterMap.remove(destination);
        if (tableMetaDataGetter != null)
        {
            tableMetaDataGetter = null;
        }
    }
}
